package org.itboys.mobile.service.mongo.web;


import org.itboys.admin.entity.lasted.SystemUser;
import org.itboys.admin.service.lasted.SystemUserService;
import org.itboys.admin.tools.AdminSessionHolder;
import org.itboys.commons.utils.encryption.Digests;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dao.score.SundayScoreConfigMapper;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.entity.mysql.score.SundayScoreConfig;
import org.itboys.mobile.entity.mysql.wallet.SundayWallet;
import org.itboys.mobile.service.mysql.web.wallet.SundayWalletService;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_用户_service
 */
@Service
public class SundayMemberService extends BaseMongoService<SundayMember> {

  @Autowired
  private SundayWalletService walletService;
    @Autowired
    private SundayScoreConfigMapper scoreConfigMapper;
    @Autowired
    private SystemUserService userService;

    @Resource(name = "mobileDs")
    private MongoDataSource mobileDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return mobileDataSource;
    }

    @Override
    protected Class<SundayMember> getEntityClass() {
        return SundayMember.class;
    }

    /**
     * 分页获取会员信息信息
     * @param request
     * @return
     */
    public PageResult<SundayMember> selectMember(HttpServletRequest request,Integer userType){
        Map<String,Object> param =ParamUtil.packageMongoExactParam(request,"");
        param.put("isDeleted", SundayCommonConstants.NO);
        if(userType!=null){
            param.put("userType",userType);
        }
        Map<String,String> containsparam = ParamUtil.packageMongoVagueParam(request,"nickName","mobile");
        PageResult<SundayMember> pageResult = super.containpageQuery(request,containsparam,param);


        //获取用户等级
        List<Long> memberIds  = new ArrayList<>();
        for(SundayMember member:pageResult.getData()){
            memberIds.add(member.getId());
        }
        if(ListUtil.isNotNull(memberIds)){
            param.clear();
            param.put("memberIds",memberIds);
            List<SundayWallet> wallets= walletService.select(param);
            param.clear();

            List<SundayScoreConfig> configs = scoreConfigMapper.select(param);
            for(SundayMember member:pageResult.getData()){
                for(SundayWallet wallet:wallets){
                    if(LongUtil.isNotZreo(wallet.getMemberId())&&
                            member.getId()==wallet.getMemberId().longValue()){
                        member.setIntegral(wallet.getIntegral());
                        member.setTotalAmount(wallet.getTotalAmount());
                        member.setReward(wallet.getReward());
                        member.setConch(wallet.getConch());
                        member.setAigouCoin(wallet.getAigouCoin());
                        String grade="";
                        for(SundayScoreConfig config:configs){
                            if(wallet.getIntegral().compareTo(config.getLine())==1){
                                grade=config.getName();
                            }
                        }
                        member.setGrade(grade);
                        break;
                    }
                }
            }
        }
        return pageResult;
    }
    /**
     * 分页获取用户信息
     * @param request
     * @return
     */
    public List<SundayMember> selectMemberNoPage(HttpServletRequest request,List<Integer> userTypes){
        Map<String,Object> param =ParamUtil.packageMongoExactParam(request,"userType");
        param.put("isDeleted", 0);
        param.put("parentId",0);
        if(ListUtil.isNotNull(userTypes)){
            param.put("userType in",userTypes);
        }
        return super.list(param);
    }



    /**
     * 重置为默认密码
     * @param id
     */
    public void resetPassword(Long id){
            super.update(id,"password",Digests.md5(SundayCommonConstants.default_password));
    }
    /**
     * 冻结和启用账号
     * @param id
     * @param status 0正常，1冻结
     */
    public void change(Long id,Integer status){

        super.update(id,"status",status);
        //2018年3月9日，冻结后台登陆账号
        SundayMember member=super.getById(id);
        if(member.getUserType()==SundayCommonConstants.user_type_business){
            Map<String,Object> param = new HashMap<String,Object>();
            param.put("isDeleted",SundayCommonConstants.NO);
            param.put("platformMemberId",id);
            List<SystemUser> users = userService.list(param);
            if(ListUtil.isNotNull(users)){
                    userService.update(users.get(0).getId(),"status",status);
            }
        }


    }


    /**
     * 提升用户等级，(更新积分)
     * @param memberId
     * @param integral
     */
    public void updateGrade(Long memberId,BigDecimal integral){
        //铭感操作判断后台是否登陆
        if(LongUtil.isNotZreo(AdminSessionHolder.getAdminUserId())){
            Map<String,Object> param = new HashMap<String,Object>();
            //List<SundayScoreConfig> configs = scoreConfigMapper.select(param);
            SundayWallet wallet = walletService.getByMemberId(memberId);
            wallet.setIntegral(wallet.getIntegral().add(integral));
            walletService.update(wallet);
        }
    }
}
